package code.example.base.util.concurrent.thread;

import code.example.tool.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.HttpClients;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;

public class Parallel {
    String url = "http://www.baidu.com";
    public Integer visitBaidu(){
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpClient httpClient = HttpClients.createDefault();
            HttpResponse httpResponse = httpClient.execute(httpGet);
            return httpResponse.getStatusLine().getStatusCode();
        }catch (Exception e){
            return -1;
        }finally {

        }
    }
    class HttpCallable implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            return visitBaidu();
        }
    }

    ExecutorService executorService =  Executors.newFixedThreadPool(16);
    int taskNumber = 10;
    public List<Callable<Integer>> getTaskList(){
        List list = new ArrayList(10);
        for(int i=0;i<taskNumber;i++){
            Callable callable = new HttpCallable();
            list.add(callable);
        }
        return list;
    }
    public void warmUp(){
        try {
            for (int i = 0; i < 10; i++) {
                new HttpCallable().call();
            }
        }catch (Exception e){};
    }
    public void testParallel(){
        long startTime = System.currentTimeMillis();
        try {
            List<Callable<Integer>> taskList = getTaskList();
            List<Future<Integer>> futureList =   executorService.invokeAll(taskList);
            for(int i=0;i<futureList.size();i++){
                Future<Integer> future = futureList.get(i);
                LogFactory.getLog().info("parallel:  result:"+future.get());
            }
        }catch (Exception e){

        }
        long useTime = System.currentTimeMillis() - startTime;
        LogFactory.getLog().info("parallel: execute all task ,use time:"+useTime);
    }

    public void testSequence(){
        long startTime = System.currentTimeMillis();
        try {
            List<Callable<Integer>> taskList = getTaskList();

            for(int i=0;i<taskList.size();i++){
                Callable<Integer> callable  = taskList.get(i);
                long oneStartTime = System.currentTimeMillis();
                Integer result = callable.call();
                long oneUseTime = System.currentTimeMillis() - oneStartTime;
                LogFactory.getLog().info("Sequence : result:"+result+",one use time:"+oneUseTime);
            }
        }catch (Exception e){

        }
        long useTime = System.currentTimeMillis() - startTime;
        LogFactory.getLog().info("Sequence :execute all task ,use time:"+useTime);
    }
    public static void main(String args []){
        Parallel parallel =   new Parallel();
        parallel.warmUp();
        parallel.testParallel();
        parallel.testSequence();

    }
}
